package com.auction.service.impl;

import com.auction.dto.AuctionCategoryPageQuery;
import com.auction.exception.BusinessException;
import com.auction.mapper.CategoryMapper;
import com.auction.pojo.AuctionCategory;
import com.auction.pojo.PageResult;
import com.auction.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * category service implements class
 *
 * @author chauncy
 * @date 2023-06-27 *
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增类目
     *
     * @param category
     */
    @Override
    public void add(AuctionCategory category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setStatus(1);

        categoryMapper.insert(category);
    }

    /**
     * 分页查询
     *
     * @param categoryPageQuery
     * @return
     */
    @Override
    public PageResult pageQuery(AuctionCategoryPageQuery categoryPageQuery) {
        PageHelper.startPage(categoryPageQuery.getPage(), categoryPageQuery.getPageSize());
        Page<AuctionCategory> page = categoryMapper.pageQuery(categoryPageQuery);

        return new PageResult<>(page.getTotal(), page.getResult());
    }


    /**
     * 根据ID查询类目
     *
     * @param id
     * @return
     */
    @Override
    public AuctionCategory getById(Long id) {
        AuctionCategory category = categoryMapper.selectById(id);
        if (category == null) {
            throw new BusinessException("类目不存在");
        }
        return category;
    }

    /**
     * 更新类目
     *
     * @param category
     */
    @Override
    public void update(AuctionCategory category) {
        AuctionCategory existingCategory = categoryMapper.selectById(category.getId());
        if (existingCategory == null) {
            throw new BusinessException("类目不存在");
        }

        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    /**
     * 启用或禁用类目
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        AuctionCategory category = categoryMapper.selectById(id);
        if (category == null) {
            throw new BusinessException("类目不存在");
        }

        category.setStatus(status);
        category.setUpdateTime(LocalDateTime.now());

        categoryMapper.update(category);
    }

    /**
     * 根据ID删除类目
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        AuctionCategory category = categoryMapper.selectById(id);
        if (category == null) {
            throw new BusinessException("类目不存在");
        }

        // 检查是否有关联的拍卖品
        int auctionCount = categoryMapper.countAuctionsByCategoryId(id);
        if (auctionCount > 0) {
            throw new BusinessException("无法删除，该类目下存在拍卖品");
        }

        categoryMapper.delete(id);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<AuctionCategory> list(Integer type) {
        return categoryMapper.list(type);
    }

}